//
//  ArrowButton.m
//  CedarFinancePH
//
//  Created by lshenrong on 2018/4/16.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "ArrowButton.h"


@interface ArrowButton()

@property (nonatomic,copy) NSString *buttonTitle;
@property (nonatomic,assign) NSInteger isButtonSelect;
@property (nonatomic,assign) NSInteger isHide;

@end

@implementation ArrowButton


+ (instancetype)buttonWithTitle:(NSString *)title   {
    return  [ArrowButton buttonWithTitle:title hideArrow:NO];
}

+ (instancetype)buttonWithTitle:(NSString *)title hideArrow:(BOOL)hide{
    ArrowButton *btn = [super buttonWithType:UIButtonTypeCustom];
    if (btn) {
        btn.arrowStatus = ArrowStatusNormal;
        btn.buttonTitle = title;
        btn.isHide = hide;
        [btn addTarget:btn action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
        [btn initializeUI];
    }
    return btn;
}

- (void)setArrowStatus:(ArrowStatus)arrowStatus{
    _arrowStatus = arrowStatus;
    if (arrowStatus == ArrowStatusNormal) {
         [self setTitleColor:kGrayColor forState:UIControlStateNormal];
        UIImage *norImage = [UIImage imageNamed:@"ArrowNormal"];
        [self setImage:norImage forState:UIControlStateNormal];
    }else if(arrowStatus == ArrowStatusUp){
        [self setTitleColor:kBlueColor forState:UIControlStateNormal];
        
    }else if(arrowStatus == ArrowStatusDown){
        [self setTitleColor:kBlueColor forState:UIControlStateNormal];
    }
}

- (void)clickButton{
   /* if (!self.isHide){
        [self setTitleColor:kOrangeColor forState:UIControlStateNormal];
        if(_arrowStatus == ArrowStatusNormal){
            _arrowStatus = ArrowStatusUp;
        }
        if(_arrowStatus == ArrowStatusUp){
            _arrowStatus = ArrowStatusDown;
            UIImage *norImage = [UIImage imageNamed:@"ArrowDown"];
            [self setImage:norImage forState:UIControlStateNormal];
        }else if(_arrowStatus == ArrowStatusDown){
            _arrowStatus = ArrowStatusUp;
            UIImage *norImage = [UIImage imageNamed:@"ArrowUp"];
            [self setImage:norImage forState:UIControlStateNormal];
        }
    }else{
        [self setTitleColor:kOrangeColor forState:UIControlStateNormal];
    }*/
    if (self.arrowStatusBlock){
        self.arrowStatusBlock(self);
    }
}

- (void)changeButtonStatus{
    if (!self.isHide){
        [self setTitleColor:kBlueColor forState:UIControlStateNormal];
        if(_arrowStatus == ArrowStatusNormal){
            _arrowStatus = ArrowStatusUp;
        }
        if(_arrowStatus == ArrowStatusUp){
            _arrowStatus = ArrowStatusDown;
            UIImage *norImage = [UIImage imageNamed:@"ArrowDown"];
            [self setImage:norImage forState:UIControlStateNormal];
        }else if(_arrowStatus == ArrowStatusDown){
            _arrowStatus = ArrowStatusUp;
            UIImage *norImage = [UIImage imageNamed:@"ArrowUp"];
            [self setImage:norImage forState:UIControlStateNormal];
        }
    }else{
        [self setTitleColor:kBlueColor forState:UIControlStateNormal];
    }
}

- (void)initializeUI{
   // self.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    [self setAdjustsImageWhenHighlighted:NO];
    [self setBackgroundColor:[UIColor clearColor]];
    self.titleLabel.font = PingFangZhongChangGuiForFont(14);
    [self setTitle:self.buttonTitle forState:UIControlStateNormal];
    [self setTitleColor:kGrayColor forState:UIControlStateNormal];
    if (!self.isHide) {
        [self setTitleColor:kMasterColor forState:UIControlStateSelected];
        UIImage *norImage = [UIImage imageNamed:@"ArrowNormal"];
        [self setImage:norImage forState:UIControlStateNormal];
        [self sizeToFit];
        self.titleEdgeInsets = UIEdgeInsetsMake(0, -self.imageView.frame.size.width, 0, self.imageView.frame.size.width);
        self.imageEdgeInsets = UIEdgeInsetsMake(0, self.titleLabel.frame.size.width+5, 0, -self.titleLabel.frame.size.width);
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
}
- (void)layoutSubviews{
    [super layoutSubviews];
    if (self.isHide) {
        self.imageView.hidden = YES;
    }
}


@end
